﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Musicman.Common.Entities;
using Musicman.Business;
using Musicman.Common.Utilities;

namespace Musicman.Client2
{
    delegate void FillFormDataHandler(SearchFile sf);

    public partial class FileStatsForm : Form
    {
        private long searchFileID;
        private WaitCallback wcbLoadData;

        public long SearchFileID
        {
            get { return searchFileID; }
            set 
            {
                if (value != searchFileID)
                {
                    searchFileID = value;
                    RefreshData();
                }
            }
        }

        public FileStatsForm()
        {
            InitializeComponent();
            KeyPreview = true;
            wcbLoadData = new WaitCallback(LoadFileData);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Hide();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void FileStatsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
        }

        public void RefreshData()
        {
            ThreadPool.QueueUserWorkItem(wcbLoadData);
        }

        private void LoadFileData(object arg)
        {
            SearchFile sf = SearchFileManager.Instance.GetSearchFile(this.searchFileID);
            sf.Statistics = SearchFileManager.Instance.LoadFileStatistics(this.searchFileID);
            PopulateFileData(sf);
        }

        private void PopulateFileData(SearchFile sf)
        {
            if (this.InvokeRequired)
            {
                FillFormDataHandler dh = new FillFormDataHandler(PopulateFileData);
                this.Invoke(dh, new object[] { sf });
            }
            else
            {
                this.lblNameValue.Text = sf.Name;
                this.lblPathValue.Text = String.Format(@"{0}\{1}", sf.FilePath, sf.Name);
                this.lblSizeValue.Text = Converter.LengthMB(sf.Length);
                this.lblDownloadedValue.Text = sf.Statistics.TotalDownloads.ToString();
                this.lblDownloadedValue.Text = sf.Statistics.TotalVoted.ToString();
                this.lblRatingValue.Text = sf.Statistics.Rating.ToString();
            }
        }

        private void btnRate_Click(object sender, EventArgs e)
        {
            int rating = 0;
            if (radioButton1.Checked)
                rating = 1;
            else if (radioButton2.Checked)
                rating = 2;
            else if (radioButton3.Checked)
                rating = 3;
            else if (radioButton4.Checked)
                rating = 4;
            else if (radioButton5.Checked)
                rating = 5;

            if (rating != 0)
            {
                SearchFileManager.Instance.UpdateFileStats(this.searchFileID, rating);
                RefreshData();
            }
        }
    }
}
